#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QChartView>
#include <QLineSeries>
#include <QTimer>
#include <QDateTime>
#include <QString>
#include <QCategoryAxis>
#include <QLabel>
#include <QTextCodec>
#include <QtCharts>
#include "../zhuTest/zhutest.h"


QT_CHARTS_USE_NAMESPACE



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);//定时器
    connect(timer,SIGNAL(timeout()),this,SLOT(timeUpdate()));//连接定时器跟TimeUpdate
    timer->start(1000);//一秒计时一次

    ZhuTest =new zhuTest();
    ZhuTest->getTodayTemperature();
    connect(ZhuTest,SIGNAL(sendTodayTemperature(QJsonObject)),this,SLOT(slot_getTodayTemperature(QJsonObject)));


}

void MainWindow::setChartWithData(QJsonArray arr){

    QLineSeries *series = new QLineSeries();

    for(int i = 0; i < arr.count(); i++)
    {
        QJsonValue   val_value =arr.at(i) ;
        QVariantMap val_map = val_value.toVariant().toMap();
        int num1 = val_map.value("time").toInt();
        int num2 = val_map.value("wendu").toInt();
        series->append(num1,num2);

        qDebug() << num1 << num2;
    }


    QChart *chart = new QChart();
    chart->addSeries(series);//关联series到Qchart
    series->setUseOpenGL(true);//开启OpenGL
    QCategoryAxis *axisX = new QCategoryAxis;//创建Ｘ轴
    axisX->setMin(0);
    axisX->setMax(24);
    axisX->setStartValue(0);
    axisX->append("0点",0);
    axisX->append("4点",4);
    axisX->append("8点",8);
    axisX->append("12点",12);
    axisX->append("16点",16);
    axisX->append("20点",20);
    axisX->append("24点",24);//为坐标轴Ｘ添加标签
    axisX->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);//让刻度线与刻度对齐
    chart->setAxisX(axisX,series);
    QCategoryAxis *axisY = new QCategoryAxis;//创建Ｙ轴
    axisY->setMin(18);
    axisY->setMax(30);
    axisY->setStartValue(18);
    axisY->append("18°C",18);
    axisY->append("20°C",20);
    axisY->append("22°C",22);
    axisY->append("24°C",24);
    axisY->append("26°C",26);
    axisY->append("28°C",28);
    axisY->append("30°C",30);//为坐标轴Ｙ添加标签
    axisY->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);//让刻度线与刻度对齐
    chart->setAxisY(axisY,series);

    chart->setTitle(QStringLiteral("24小时温度曲线图"));//设置折线图标题
    QChartView *view1= new QChartView(chart,this);//显示折线图
    view1->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);//去掉折线图边框
    view1->move(0,270);//移动折线图位置
    view1->setRenderHint(QPainter::Antialiasing);//开启抗锯齿
    view1->resize(790, 400);//设置尺寸大小
    view1->show();

}

void MainWindow::setWeatherWithData(QJsonArray arr1){

    QList<QString> list;
    for(int i = 0; i < arr1.count(); i++)
    {
        QJsonValue val_value =arr1.at(i) ;
        QVariantMap val_map = val_value.toVariant().toMap();
        QString str1 = val_map.value("time").toString();
        QString str2 = val_map.value("dayName").toString();
        QString str3 = val_map.value("temperture").toString();
        QString str4 = val_map.value("weather").toString();
        QString str5 = val_map.value("quality").toString();
        QString str6 = val_map.value("wind").toString();
        QString str = str1+"\n"+str2 +"\n"+str3+"\n"+str4 +"\n"+str5 +"\n"+str6;
        list.append(str);
     }
    day1 = new QLabel(this);//创建ｌａｂｅｌ－ｄａｙ１
    day1->setText(list[0]);
    day1->resize(250,250);//设置尺寸大小
    day1->move(0,20);//设置放置位置
    day1->setAlignment(Qt::AlignCenter);//文字居中显示
    day1->setScaledContents(true);//内容缩放对齐
    QFont fontDay1;
    fontDay1.setPointSize(15);
    day1->setFont(fontDay1);//设置字号
    day1->show();


    day2 = new QLabel(this);
    day2->setText(list[1]);
    day2->resize(180,250);
    day2->move(250,20);
    day2->setAlignment(Qt::AlignCenter);
    day2->setScaledContents(true);
    QFont fontDay2;
    fontDay2.setPointSize(15);
    day2->setFont(fontDay2);
    day2->show();//ｌａｂｅｌ－ｄａｙ２同上


    QLabel *day3 = new QLabel(this);
    day3->setText(list[2]);
    day3->resize(180,250);
    day3->move(430,20);
    day3->setAlignment(Qt::AlignCenter);
    day3->setScaledContents(true);
    QFont fontDay3;
    fontDay3.setPointSize(15);
    day3->setFont(fontDay3);
    day3->show();//ｌａｂｅｌ－ｄａｙ３同上


    QLabel *day4 = new QLabel(this);
    day4->setText(list[3]);
    day4->resize(180,250);
    day4->move(610,20);
    day4->setAlignment(Qt::AlignCenter);
    day4->setScaledContents(true);
    QFont fontDay4;
    fontDay4.setPointSize(15);
    day4->setFont(fontDay4);
    day4->show();//ｌａｂｅｌ－ｄａｙ４>同上
    qApp->processEvents();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::timeUpdate()
{
    QDateTime time = QDateTime::currentDateTime();//读取当前时间到
    QString dateTime = time.toString("yyyy-MM-dd hh:mm:ss dddd");//将当前时间转换成字符串
    ui->LabelDateTime->setText(dateTime);//打印字符串，显示当前时间

}

void MainWindow::slot_getTodayTemperature(QJsonObject obj)
{
    QJsonArray arr = obj.take("chartData").toArray();
    qDebug() << arr;
    this->setChartWithData(arr);

    QJsonArray arr1 = obj.take("weatherData").toArray();
    this->setWeatherWithData(arr1);
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.drawPixmap(0,ui->mainToolBar->height(),this->width(),this->height()-
                       ui->mainToolBar->height()-ui->statusBar->height(),QPixmap("/root/background1.png"));
}
